package com.system.util;
import com.system.entity.Grade;
import com.system.entity.Takecourse;
import jxl.CellView;
import jxl.Workbook;
import jxl.write.*;


import java.io.File;
import java.util.List;
public class ExportExcel {
    public void ExportExcel(){

    }
    public void InExcel2(List<Takecourse> list,String fileName){
        try {
            WritableFont fontTitle = new WritableFont(
                    WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD);
            WritableCellFormat formatTitle = new WritableCellFormat(fontTitle);
            formatTitle.setWrap(true);
            CellView c = new CellView();
            c.setSize(25 * 256);
            c.setFormat(formatTitle);
            WritableWorkbook wwb = null;
            // 创建可写入的Excel工作簿
//          String fileName = "D://book.xls";
            File file=new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            //以fileName为文件名来创建一个Workbook
            wwb = Workbook.createWorkbook(file);
            // 创建工作表
            WritableSheet ws = wwb.createSheet("学生成绩", 0);
            //查询数据库中所有的数据
            ws.setColumnView(0,c);
            ws.setColumnView(1,c);
            ws.setColumnView(2,c);
            ws.setColumnView(3,c);
            ws.setColumnView(4,c);
            ws.setColumnView(6,c);
//            List<Takecourse> list = takeCourseService.findByCourseID(stu_id,course_id);
            //要插入到的Excel表格的行号，默认从0开始
            Label labelId= new Label(0, 0, "学号");//表示第
            Label labelName= new Label(1, 0, "姓名");
            Label labelusualP = new Label(2, 0, "平时分");
            Label labelExperP= new Label(3, 0, "实验分");
            Label labelFinalP= new Label(4, 0, "期末分");
            Label labeltotalP = new Label(5, 0, "总分");
            ws.addCell(labelId);
            ws.addCell(labelName);
            ws.addCell(labelusualP);
            ws.addCell(labelExperP);
            ws.addCell(labelFinalP);
            ws.addCell(labeltotalP);
            for (int i = 0; i < list.size(); i++) {
                Label labelId_i= new Label(0, i+1, list.get(i).getStu_id()+"");
                Label labelName_i= new Label(1, i+1, list.get(i).getStu_name());
                Label labelUsalP_i= new Label(2, i+1, "");
                Label labelExperP_i= new Label(3, i+1, "");
                Label labelFinalP_i= new Label(4, i+1, "");
                Label labelTotalP_i = new Label(5,i+1,"");
                ws.addCell(labelId_i);
                ws.addCell(labelName_i);
                ws.addCell(labelUsalP_i);
                ws.addCell(labelExperP_i);
                ws.addCell(labelFinalP_i);
                ws.addCell(labelTotalP_i);
            }
            //写进文档
            wwb.write();
            // 关闭Excel工作簿对象
            wwb.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void InExcel1(List<Grade> list,String fileName){
        try {
            WritableFont fontTitle = new WritableFont(
                    WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD);
            WritableCellFormat formatTitle = new WritableCellFormat(fontTitle);
            formatTitle.setWrap(true);
            CellView c = new CellView();
            c.setSize(25 * 256);
            c.setFormat(formatTitle);
            WritableWorkbook wwb = null;
            // 创建可写入的Excel工作簿
//          String fileName = "D://book.xls";
            File file=new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            //以fileName为文件名来创建一个Workbook
            wwb = Workbook.createWorkbook(file);
            // 创建工作表
            WritableSheet ws = wwb.createSheet("学生成绩", 0);
            ws.setColumnView(0,c);
            ws.setColumnView(1,c);
            ws.setColumnView(2,c);
            ws.setColumnView(3,c);
            ws.setColumnView(4,c);
            ws.setColumnView(6,c);
            //查询数据库中所有的数据
//            List<Takecourse> list = takeCourseService.findByCourseID(stu_id,course_id);
            //要插入到的Excel表格的行号，默认从0开始
            Label labelsemester= new Label(0, 0, "学年学期");
            Label labelcoding= new Label(1, 0, "课程编号");
            Label labelCname= new Label(2, 0, "课程名称");
            Label labelcredit = new Label(7, 0, "课程学分");
            Label labelcteacher = new Label(8, 0, "授课老师");
            Label labelusualP = new Label(3, 0, "平时分");
            Label labelExperP= new Label(4, 0, "实验分");
            Label labelFinalP= new Label(5, 0, "期末分");
            Label labeltotalP = new Label(6, 0, "总分");
            ws.addCell(labelsemester);
            ws.addCell(labelcoding);
            ws.addCell(labelCname);
            ws.addCell(labelusualP);
            ws.addCell(labelExperP);
            ws.addCell(labelFinalP);
            ws.addCell(labeltotalP);
            ws.addCell(labelcredit);
            ws.addCell(labelcteacher);
            for (int i = 0; i < list.size(); i++) {
//                Label labelName_i= new Label(1, i+1, list.get(i).get);
                Label labelsemester_i= new Label(0, i+1, list.get(i).getSemester()+"");
                Label labelcoding_i = new Label(1,i+1,list.get(i).getCourse_coding()+"");
                Label labelName_i = new Label(2,i+1,list.get(i).getCourse_name()+"");
                Label labelUsalP_i= new Label(3, i+1, list.get(i).getUsual_performance()+"");
                Label labelExperP_i= new Label(4, i+1, list.get(i).getExperience()+"");
                Label labelFinalP_i= new Label(5, i+1, list.get(i).getFinal_performance()+"");
                Label labelTotalP_i = new Label(6,i+1,list.get(i).getTotal_performance()+"");
                Label labelcredit_i = new Label(7,i+1,list.get(i).getCredit()+"");
                Label labelteacher_i = new Label(8,i+1,list.get(i).getTea_name());
                ws.addCell(labelsemester_i);
                ws.addCell(labelcoding_i);
                ws.addCell(labelName_i);
                ws.addCell(labelUsalP_i);
                ws.addCell(labelExperP_i);
                ws.addCell(labelFinalP_i);
                ws.addCell(labelTotalP_i);
                ws.addCell(labelcredit_i);
                ws.addCell(labelteacher_i);
            }
            //写进文档
            wwb.write();
            // 关闭Excel工作簿对象
            wwb.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  void InExcel(List<Takecourse> list,String fileName) {
        try {
            WritableFont fontTitle = new WritableFont(
                    WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD);
            WritableCellFormat formatTitle = new WritableCellFormat(fontTitle);
            formatTitle.setWrap(true);
            CellView c = new CellView();
            c.setSize(25 * 256);
            c.setFormat(formatTitle);
            WritableWorkbook wwb = null;
            // 创建可写入的Excel工作簿
//          String fileName = "D://book.xls";
            File file=new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            //以fileName为文件名来创建一个Workbook
            wwb = Workbook.createWorkbook(file);
            // 创建工作表
            WritableSheet ws = wwb.createSheet("学生成绩", 0);
            ws.setColumnView(0,c);
            ws.setColumnView(1,c);
            ws.setColumnView(2,c);
            ws.setColumnView(3,c);
            ws.setColumnView(4,c);
            ws.setColumnView(6,c);
            //查询数据库中所有的数据
//            List<Takecourse> list = takeCourseService.findByCourseID(stu_id,course_id);
            //要插入到的Excel表格的行号，默认从0开始
            Label labelId= new Label(0, 0, "学号");//表示第
            Label labelName= new Label(1, 0, "姓名");
            Label labelusualP = new Label(2, 0, "平时分");
            Label labelExperP= new Label(3, 0, "实验分");
            Label labelFinalP= new Label(4, 0, "期末分");
            Label labeltotalP = new Label(5, 0, "总分");
            ws.addCell(labelId);
            ws.addCell(labelName);
            ws.addCell(labelusualP);
            ws.addCell(labelExperP);
            ws.addCell(labelFinalP);
            ws.addCell(labeltotalP);
            for (int i = 0; i < list.size(); i++) {
                Label labelId_i= new Label(0, i+1, list.get(i).getStu_id()+"");
                Label labelName_i= new Label(1, i+1, list.get(i).getStu_name());
                Label labelUsalP_i= new Label(2, i+1, list.get(i).getUsualPerformance()+"");
                Label labelExperP_i= new Label(3, i+1, list.get(i).getExperience()+"");
                Label labelFinalP_i= new Label(4, i+1, list.get(i).getFinalPerformance()+"");
                Label labelTotalP_i = new Label(5,i+1,list.get(i).getTotalPerformance()+"");
                ws.addCell(labelId_i);
                ws.addCell(labelName_i);
                ws.addCell(labelUsalP_i);
                ws.addCell(labelExperP_i);
                ws.addCell(labelFinalP_i);
                ws.addCell(labelTotalP_i);
            }
            //写进文档
            wwb.write();
            // 关闭Excel工作簿对象
            wwb.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
